package cn.edu.tju.elm.service;

import cn.edu.tju.core.model.User;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.Business;
import cn.edu.tju.elm.repository.BusinessRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    
    @Autowired
    private UserService userService;

    /**
     * 保存商家信息
     * @param business 商家对象
     * @return 保存后的商家对象
     */
    public Business saveBusiness(Business business) {
        if (business.getBusinessName() == null || business.getBusinessName().trim().isEmpty()) {
            throw new IllegalArgumentException("商家名称不能为空");
        }
        
        // 如果businessOwner只有ID，需要填充完整的User对象
        if (business.getBusinessOwner() != null && business.getBusinessOwner().getId() != null) {
            // 验证用户是否存在
            Optional<User> userOpt = userService.getUserById(business.getBusinessOwner().getId());
            if (userOpt.isPresent()) {
                business.setBusinessOwner(userOpt.get());
            } else {
                throw new IllegalArgumentException("指定的商家所有者不存在");
            }
        } else {
            throw new IllegalArgumentException("商家所有者不能为空");
        }
        
        return businessRepository.save(business);
    }
    
    /**
     * 获取所有商家列表
     * @return 商家列表
     */
    public List<Business> getAllBusinesses() {
        return businessRepository.findAll();
    }
    
    /**
     * 根据ID获取商家信息
     * @param id 商家ID
     * @return 商家对象
     */
    public Optional<Business> getBusinessById(Long id) {
        return businessRepository.findById(id);
    }
    
    /**
     * 更新商家信息
     * @param id 商家ID
     * @param businessDetails 更新的商家信息
     * @return 更新后的商家对象
     */
    public Business updateBusiness(Long id, Business businessDetails) {
        Optional<Business> business = businessRepository.findById(id);
        if (business.isPresent()) {
            Business existingBusiness = business.get();
            existingBusiness.setBusinessName(businessDetails.getBusinessName());
            existingBusiness.setBusinessAddress(businessDetails.getBusinessAddress());
            existingBusiness.setBusinessExplain(businessDetails.getBusinessExplain());
            existingBusiness.setBusinessImg(businessDetails.getBusinessImg());
            existingBusiness.setOrderTypeId(businessDetails.getOrderTypeId());
            existingBusiness.setStartPrice(businessDetails.getStartPrice());
            existingBusiness.setDeliveryPrice(businessDetails.getDeliveryPrice());
            existingBusiness.setRemarks(businessDetails.getRemarks());
            return businessRepository.save(existingBusiness);
        }
        return null;
    }
    
    /**
     * 删除商家信息
     * @param id 商家ID
     * @return 是否删除成功
     */
    public boolean deleteBusiness(Long id) {
        Optional<Business> business = businessRepository.findById(id);
        if (business.isPresent()) {
            businessRepository.deleteById(id);
            return true;
        }
        return false;
    }

    /**
     * 根据用户ID获取商家信息
     * @param userId 用户ID
     * @return 商家对象，如果不存在则返回null
     */
    public Business getBusinessByUserId(Long userId) {
        return businessRepository.findByBusinessOwner_Id(userId).orElse(null);
    }
}
